home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / java / threads / betaclasses / Clock.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  673 b   |  33 lines

  1.  
  2. import java.awt.Graphics;
  3. import java.util.Date;
  4.  
  5. public class Clock extends java.applet.Applet implements Runnable {
  6.  
  7.     Thread clockThread;
  8.  
  9.     public void start() {
  10.     if (clockThread == null) {
  11.         clockThread = new Thread(this, "Clock");
  12.         clockThread.start();
  13.     }
  14.     }
  15.     public void run() {
  16.     while (clockThread != null) {
  17.         repaint();
  18.         try {
  19.         clockThread.sleep(1000);
  20.         } catch (InterruptedException e){
  21.         }
  22.     }
  23.     }
  24.     public void paint(Graphics g) {
  25.     Date now = new Date();
  26.     g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10);
  27.     }
  28.     public void stop() {
  29.     clockThread.stop();
  30.     clockThread = null;
  31.     }
  32. }
  33.